home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / libiberty / strtol.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  1KB  |  55 lines

  1. /*
  2.  * strtol : convert a string to long.
  3.  *
  4.  * Andy Wilson, 2-Oct-89.
  5.  */
  6. #include <errno.h>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include "ansidecl.h"
  10.  
  11. /* FIXME: It'd be nice to configure around these, but the include files are too
  12.    painful */
  13. #define    LONG_MIN    0x80000000
  14. #define    LONG_MAX    0x7FFFFFFF
  15. #define    ULONG_MAX    0xFFFFFFFF
  16.  
  17. extern int errno;
  18.  
  19. long
  20. strtol(s, ptr, base)
  21.      CONST char *s; char **ptr; int base;
  22. {
  23.   extern unsigned long  strtoul();
  24.   int minus=0;
  25.   unsigned long tmp;
  26.   CONST char *start=s, *eptr;
  27.  
  28.   if (s==NULL)
  29.     {
  30.       errno = ERANGE;
  31.       if (!ptr)
  32.     *ptr = (char *)start;
  33.       return 0L;
  34.     }
  35.   while (isspace(*s))
  36.     s++;
  37.   if (*s == '-') {
  38.     s++;
  39.     minus = 1;
  40.       }
  41.   else if (*s == '+')
  42.     s++;
  43.  
  44.   /*
  45.    * let strtoul do the hard work.
  46.    */
  47.   tmp = strtoul(s, &(char *)eptr, base);
  48.   if (ptr != NULL)
  49.     *ptr = (char *)((eptr==s) ? (char *)start : eptr);
  50.   if (errno==ERANGE && tmp==ULONG_MAX)
  51.     return (minus ? LONG_MIN : LONG_MAX);
  52.   else
  53.     return (minus ? (long) -tmp : (long) tmp);
  54. }
  55.